package device;


import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
import memory.PCBData;


public class DeviceUIcontroller {
    @FXML private Button btnB1;
    @FXML private Button btnC2;
    @FXML private Button btnC1;
    @FXML private Label labelC1;
    @FXML private Button btnA2;
    @FXML private Button btnB3;
    @FXML private Button btnA1;
    @FXML private Button btnB2;
    @FXML private Label labelC2;
    @FXML private Button btnC3;
    @FXML private Label labelB1;
    @FXML private Label labelC3;
    @FXML private Label labelA2;
    @FXML private Label labelB3;
    @FXML private Label labelA1;
    @FXML private Label labelB2;
    @FXML private Label LabelA;
    @FXML private Label LabelB;
    @FXML private Label LabelC;
    
    public void initialize() {
    	btnA1.setGraphic(getIcon("/image/A.png"));
    	btnA2.setGraphic(getIcon("/image/A.png"));
    	btnB1.setGraphic(getIcon("/image/B.png"));
    	btnB2.setGraphic(getIcon("/image/B.png"));
    	btnB3.setGraphic(getIcon("/image/B.png"));
    	btnC1.setGraphic(getIcon("/image/C.png"));
    	btnC2.setGraphic(getIcon("/image/C.png"));
    	btnC3.setGraphic(getIcon("/image/C.png")); 
    	
    	//timeline实时刷新
    			Timeline timeline = new Timeline();
    		     timeline.setCycleCount(Timeline.INDEFINITE);
    		  	 timeline.setAutoReverse(true);
    		  	 Duration duration = Duration.seconds(1); 
    		  	 EventHandler<ActionEvent> onFinished = (ActionEvent r) -> {
    		  		 update();
    				};
    			KeyFrame keyFrame = new KeyFrame(duration, onFinished);
    			  timeline.getKeyFrames().addAll(keyFrame);   
    			  timeline.play();
    	}
	
    
    public void update() {
    	//更新正在占用设备的信息
    		int pid ;
    		pid = DeviceManage.devices.get(0).getPid();
    		if(pid!=-1) {
    		labelA1.setText("\n进程"+pid+"占用-----\n");
    		btnA1.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelA1.setText("空闲");
    			btnA1.setStyle("-fx-background-color:white");
    		}
    		pid = DeviceManage.devices.get(1).getPid();
    		if(pid!=-1) {
    		labelA2.setText("\n进程"+pid+"占用-----\n");
    		btnA2.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelA2.setText("空闲");
    			btnA2.setStyle("-fx-background-color:white");
    		}
    		pid = DeviceManage.devices.get(2).getPid();
    		if(pid!=-1) {
    		labelB1.setText("\n进程"+pid+"占用-----\n");
    		btnB1.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelB1.setText("空闲");
    			btnB1.setStyle("-fx-background-color:white");
    		}
    		pid = DeviceManage.devices.get(3).getPid();
    		if(pid!=-1) {
    		labelB2.setText("\n进程"+pid+"占用-----\n");
    		btnB2.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelB2.setText("空闲");
    			btnB2.setStyle("-fx-background-color:white");
    		}
    		pid = DeviceManage.devices.get(4).getPid();
    		if(pid!=-1) {
    		labelB3.setText("\n进程"+pid+"占用-----\n");
    		btnB3.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelB3.setText("空闲");
    			btnB3.setStyle("-fx-background-color:white");
    		}
    		pid = DeviceManage.devices.get(5).getPid();
    		if(pid!=-1) {
    		labelC1.setText("\n进程"+pid+"占用-----\n");
    		btnC1.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelC1.setText("空闲");
    			btnC1.setStyle("-fx-background-color:white");
    		}
    		pid = DeviceManage.devices.get(6).getPid();
    		if(pid!=-1) {
    		labelC2.setText("\n进程"+pid+"占用-----\n");
    		btnC2.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelC2.setText("空闲");
    			btnC2.setStyle("-fx-background-color:white");
    		}
    		pid = DeviceManage.devices.get(7).getPid();
    		if(pid!=-1) {
    		labelC3.setText("\n进程"+pid+"占用-----\n");
    		btnC3.setStyle("-fx-background-color:red");
    		}
    		else {
    			labelC3.setText("空闲");
    			btnC3.setStyle("-fx-background-color:white");
    		}
    		//更新正在等待设备的信息
    		String infoA="",infoB="",infoC="";
    		for(int i=0;i<PCBData.blockQueue.size();i++) {
    			if(PCBData.blockQueue.get(i).getType()=='A') {
    				infoA += "进程"+PCBData.blockQueue.get(i).getPid()+" ";
    			}
    			else if(PCBData.blockQueue.get(i).getType()=='B') {
    				infoB += "进程"+PCBData.blockQueue.get(i).getPid()+" ";
    			}
    			else if(PCBData.blockQueue.get(i).getType()=='B') {
    				infoC += "进程"+PCBData.blockQueue.get(i).getPid()+" ";
    			}
    		}
    		if(infoA.equals(""))
    			LabelA.setText("无");
    		else 
    			LabelA.setText(infoA);
    		if(infoB.equals(""))
    			LabelB.setText("无");
    		else 
    			LabelB.setText(infoB);
    		if(infoC.equals(""))
    			LabelC.setText("无");
    		else
    			LabelC.setText(infoC);
	}
    private ImageView getIcon(String iconFileName) {    
        ImageView  icon = new ImageView(new Image(iconFileName));
        icon.setFitWidth(100);
        icon.setFitHeight(100);
        return icon;
    }

}
